package com.xingge.sdktest.convert;

import android.content.Context;

import com.jolimark.UsbTool;

import java.io.File;
import java.util.concurrent.LinkedBlockingDeque;

public class ConvertImgThread extends Thread {


    private boolean cancel;
    private PrintThread printThread;
    private LinkedBlockingDeque<ImgTask> taskQueue = new LinkedBlockingDeque<>();
    private Context context;

    public ConvertImgThread(Context context) {
        this.context = context;
    }

    public synchronized void putTask(ImgTask task) {
        try {
            taskQueue.put(task);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void bindPrintThread(PrintThread thread) {
        printThread = thread;
    }


    public synchronized void cancelTask() {
        cancel = true;
    }

    @Override
    public void run() {
        super.run();
        UsbTool.logd("convertImg thread -> 线程启动");
        while (!cancel) {
            if (!taskQueue.isEmpty()) {
                ImgTask task = taskQueue.poll();
                //任务没有结束
                if (!task.flag_end) {
                    byte[] data = null;
                    int[] pageIndex = task.pageIndex;
                    //单面打印
                    if (task.sideType == PrintConverter.SideType.SINGLE) {
                        String fileName = (String) task.taskObject;
                        PrintConverter.PrintParams printParams = task.printParams;
                        data = PrintConverter.convertImg(context, fileName, printParams);
                        UsbTool.logd("convertImg thread->转换图片 第" + pageIndex[0] + "页(" + fileName + ") 完成");
                        File file = new File(fileName);
                        if (file.exists())
                            file.delete();
                    }
                    //双面打印
                    else if (task.sideType == PrintConverter.SideType.DOUBLE) {
                        String[] fileNames = (String[]) task.taskObject;
                        PrintConverter.PrintParams printParams = task.printParams;
                        //如果凑齐双面，则双面转换
                        if (fileNames[1] != null) {
                            data = PrintConverter.convertImg(context, fileNames[0], fileNames[1], printParams);
                            UsbTool.logd("convertImg thread->转换图片 第" + pageIndex[0] + "页（" + fileNames[0] + "）与 第" + pageIndex[1] + "页（" + fileNames[1] + "） 完成");
                            for (String name : fileNames) {
                                File file = new File(name);
                                if (file.exists())
                                    file.delete();
                            }
                        }
                        //如果最后有剩余一面，则单面转换
                        else {
                            data = PrintConverter.convertImg(context, fileNames[0], printParams);
                            UsbTool.logd("convertImg thread->转换图片  第" + pageIndex[0] + "页(" + fileNames[0] + " 完成");
                            File file = new File(fileNames[0]);
                            if (file.exists())
                                file.delete();
                        }
                    }

                    if (printThread != null) {
                        PrintThread.PrintTask printTask = new PrintThread.PrintTask();
                        printTask.data = data;
                        printTask.pageIndex = task.pageIndex;
                        printTask.flag_end = false;
                        printThread.putTask(printTask);
                    }

                }
                //任务结束
                else {
                    if (printThread != null) {
                        PrintThread.PrintTask printTask = new PrintThread.PrintTask();
                        printTask.flag_end = true;
                        printThread.putTask(printTask);
                    }
                    UsbTool.logd("convertImg thread -> 全部任务完成，线程结束");
                    break;
                }
            }

        }

        if (cancel) {
            UsbTool.logd("convertImg thread -> 任务取消，清空任务队列，线程结束");
            taskQueue.clear();
        }

    }

    static class ImgTask {
        PrintConverter.SideType sideType;
        Object taskObject;
        PrintConverter.PrintParams printParams;
        int[] pageIndex;
        boolean flag_end;
    }

}
